package com.ttc.ssm.controller.home;

import com.ttc.ssm.common.JobPage;
import com.ttc.ssm.common.ResumePage;
import com.ttc.ssm.common.SearchJobPage;
import com.ttc.ssm.po.*;
import com.ttc.ssm.service.admin.CommonService;
import com.ttc.ssm.service.admin.JobService;
import com.ttc.ssm.service.home.HomeCompanyService;
import com.ttc.ssm.service.home.HomeJobService;
import com.ttc.ssm.service.home.HomeResumeService;
import com.ttc.ssm.utils.DateUtils;
import com.ttc.ssm.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Hui on 2016/8/9.
 */

@Controller
@RequestMapping("home/job")
public class HomeJobController {
    @Autowired
    HomeJobService homeJobService;

    @Autowired
    JobService jobService;

    @Autowired
    CommonService commonService;

    @Autowired
    HomeResumeService homeResumeService;

    @Autowired
    HomeCompanyService homeCompanyService;

    @RequestMapping("create")
    public String create(HttpSession session ,Model model) throws Exception{
        List<JobProperty> jobPropertyList = homeJobService.getJobProperty();
        model.addAttribute("jobPropertyList",jobPropertyList);
        List<Industry> industryList = homeJobService.getIndustry();
        model.addAttribute("industryList",industryList);
        List<JobType> jobTypeList = homeJobService.getJobTypeByIndustryId(industryList.get(0).getIndustry_id());
        model.addAttribute("jobTypeList",jobTypeList);
        List<Degree> degreeList = homeJobService.getDegree();
        model.addAttribute("degreeList",degreeList);
        List<SalaryType> salaryTypeList = homeJobService.getSalaryType();
        model.addAttribute("salaryTypeList",salaryTypeList);
        int com = (Integer) session.getAttribute("company_id");
        List<JobTag> jobTagList = homeJobService.getJobTab(com);
        model.addAttribute("jobTagList",jobTagList);
        List<Zone> zoneList = homeJobService.getZone();
        model.addAttribute("zoneList",zoneList);
        List<City> cityList = homeJobService.getCityByZoneId(zoneList.get(0).getZone_id());
        model.addAttribute("cityList",cityList);
       return "home/job/create";
    }

    @RequestMapping("preview")
    public String preview(HttpSession session , Model model ,Preview preview , @RequestParam("chestr") List chestr) throws Exception{

//         model.addAttribute("job_property_id",job_property_id);
//        model.addAttribute("job_name",job_name);
//        model.addAttribute("industry_name",industry_name);
//        model.addAttribute("job_type_name",job_type_name);
//        model.addAttribute("zone",zone);
//        model.addAttribute("city",city);
//        model.addAttribute("number",number);
//        model.addAttribute("degree",degree);
//        model.addAttribute("work_year",work_year);
//        model.addAttribute("salary_type",salary_type);
//        model.addAttribute("show_salary",show_salary);
//        model.addAttribute("jobDescription",jobDescription);
//        model.addAttribute("tag",tag);
//        model.addAttribute("address",address);
//        model.addAttribute("is_publish",is_publish);
//        model.addAttribute("startDate",startDate);
//        model.addAttribute("endDate",endDate);
        int com = (Integer) session.getAttribute("company_id");
        List<JobTag> jobTagList = homeJobService.getJobTab(com);
        Company company = homeCompanyService.selectCompanyById(com);
        model.addAttribute("company",company);
        CompanyDetail companyDetail = homeCompanyService.selectCompanyDetail(com);
        model.addAttribute("companyDetail",companyDetail);
        model.addAttribute("jobTagList",jobTagList);
        model.addAttribute("preview",preview);
        model.addAttribute("chestr",chestr);
       // @RequestParam("job_name") String job_name ,@RequestParam("job_property_id") String job_property_id , @RequestParam("job_description") String job_description  , @RequestParam("industry_name") String industry_name , @RequestParam("job_type_name") String job_type_name , @RequestParam("zone") String zone , @RequestParam("city") String city , @RequestParam("number") int number , @RequestParam("degree") String degree , @RequestParam("work_year") int work_year , @RequestParam("salary_type") String salary_type , @RequestParam("show_salary") int show_salary , @RequestParam("job_description") String jobDescription , @RequestParam("tag") List tag , @RequestParam("address") String address , @RequestParam("is_publish") int is_publish , @RequestParam("startDate") String startDate , @RequestParam("endDate") String endDate
        return "home/job/preview";
    }

    @RequestMapping("/changeJobType")
    @ResponseBody
    public List<JobType> changeJobType(@RequestParam("industry_id") int industry_id) throws Exception{
        return  homeJobService.getJobTypeByIndustryId(industry_id);
    }

    @RequestMapping("/changeCity")
    @ResponseBody
    public List<City> changeCity(@RequestParam("zone_id") int zone_id) throws Exception{
        return  homeJobService.getCityByZoneId(zone_id);
    }

    @RequestMapping("/insertJob")
    public String insertJob(Model model, HttpSession session , Job job , @RequestParam("job_name") String jobName , @RequestParam("number") int number , @RequestParam("job_description") String jobDescription , @RequestParam("address") String address) throws Exception{
        try{
            if(TextUtils.isEmpty(jobName)){
                model.addAttribute("msg",1);
                return "forward:/home/customer/create.action";
            }
            if(TextUtils.isEmpty(String.valueOf(number))){
                model.addAttribute("msg",1);
                return "forward:/home/customer/create.action";
            }
            if(TextUtils.isEmpty(jobDescription)){
                model.addAttribute("msg",1);
                return "forward:/home/customer/create.action";
            }
            if(TextUtils.isEmpty(address)){
                model.addAttribute("msg",1);
                return "forward:/home/customer/create.action";
            }
            int company_id = (Integer) session.getAttribute("company_id");
            int status = homeCompanyService.selectStatusByCompanyId(company_id);
            int detailStatus = homeCompanyService.selectDetailStatusByCompanyId(company_id);
            if (status == 1 && detailStatus == 1) {
                job.setDate_start(DateUtils.stringToDate(job.getDate_start_string()));
                job.setDate_end(DateUtils.stringToDate(job.getDate_end_string()));
                homeJobService.insertJob(company_id,job);

                model.addAttribute("success",1);
                return "forward:/home/job/create.action";
            }else {
                model.addAttribute("status",status);
                model.addAttribute("detailStatus",detailStatus);
                return "forward:/home/job/create.action";
            }

        }catch (Exception e){

            model.addAttribute("error",1);
            return "forward:/home/job/create.action";
        }
    }

    @RequestMapping("index")
    public String index(Model model) throws Exception
    {
        return "home/job/index";
    }

    @RequestMapping("/getJobList")
    @ResponseBody
    public Map getJobList(HttpSession session, @RequestParam("p") Integer page,@RequestParam("job_name") String job_name, @RequestParam("select") Integer select) throws Exception
    {
        JobPage jobPage = new JobPage();
        jobPage.setPageSize(20);
        jobPage.setCurrentPage(page);
        if (select == 1) {
            jobPage.setStatus(1);
            jobPage.setIs_publish(1);
            jobPage.setDate(new Date());
            jobPage.setSelect(1);
        }else if (select == 2) {
            jobPage.setStatus(-1);
            jobPage.setIs_publish(0);
        }
        else if (select == 3) {
            jobPage.setStatus(-1);
            jobPage.setIs_publish(-1);
            jobPage.setDate(new Date());
            jobPage.setSelect(3);
        }
        else if (select == 4) {
            jobPage.setStatus(0);
            jobPage.setIs_publish(1);
        }else if (select == 5) {
            jobPage.setStatus(2);
            jobPage.setIs_publish(-1);
        }
        jobPage.setJob_name(job_name);
        int company_id = (Integer) session.getAttribute("company_id");
        System.out.println("================>"+company_id);
        if (company_id > 0) {
            jobPage.setCompany_id(company_id);
        }
        jobPage.setTotalRows(homeJobService.countRows(jobPage));
        List<Job> jobs = homeJobService.selectJobForPage(jobPage);
        int[] delivery_num= new int[jobs.size()];
        int[] invite_num= new int[jobs.size()];
        for (int i=0;i<jobs.size();i++) {
            delivery_num[i] = homeJobService.countDeliveryRowsByJobId(jobs.get(i).getJob_id());
            invite_num[i] = homeJobService.countInviteRowsByJobId(jobs.get(i).getJob_id());
        }
        Map map = new HashMap();
        map.put("data",jobs);
        map.put("show",jobPage);
        map.put("delivery_num",delivery_num);
        map.put("invite_num",invite_num);
        return map;
    }

    @RequestMapping("/deleteJobs")
    @ResponseBody
    public int deleteJobs(@RequestBody Map ids) throws Exception{
        try {
            jobService.deleteJobs(ids);
            return 1;
        }catch (Exception e){
            System.out.println(e);
            return 0;
        }
    }
    @RequestMapping("detail")
    public String detail (HttpSession session , Model model , @RequestParam("job_id") int jobId ) throws Exception
    {
        Job job = homeJobService.selectJobById(jobId);
        model.addAttribute("job",job);
        System.out.println(job.getCity().getName());
        int ZoneId = homeJobService.getZoneByCityId(job.getCity_id());
        model.addAttribute("ZoneId",ZoneId);
        String zoneName = homeJobService.getZoneNameById(ZoneId);
        model.addAttribute("zoneName",zoneName);
        JobType jobType = homeJobService.getJobTypeByJobTypeId(job.getJob_type_id());
        model.addAttribute("jobType",jobType);
        String industryName = homeJobService.getIndustryNameById(jobType.getIndustry_id());
        model.addAttribute("industryName",industryName);
        int com = (Integer) session.getAttribute("company_id");
        List<JobTag> jobTagList = homeJobService.getJobTab(com);
        model.addAttribute("jobTagList",jobTagList);
        List<TagToJob> tagToJobList = homeJobService.getTagForJob(jobId);
        model.addAttribute("tagToJobList",tagToJobList);
        Company company = homeCompanyService.selectCompanyById(com);
        model.addAttribute("company",company);
        CompanyDetail companyDetail = homeCompanyService.selectCompanyDetail(com);
        model.addAttribute("companyDetail",companyDetail);
//        int[] jobTagId = new int[tagToJobList.size()];
//        for(int i=0;i<tagToJobList.size();i++){
//             jobTagId[i] = tagToJobList.get(i).getTag_id();
//        }
//        model.addAttribute("jobTagId",jobTagId);
        return "home/job/detail";
    }

    @RequestMapping("edit")
    public String edit(Model model, @RequestParam("job_id") Integer job_id) throws Exception
    {
        Job job = homeJobService.selectJobById(job_id);
        model.addAttribute("job",job);
        List<JobProperty> jobPropertyList = homeJobService.getJobProperty();
        model.addAttribute("jobPropertyList",jobPropertyList);
        List<Industry> industryList = homeJobService.getIndustry();
        model.addAttribute("industryList",industryList);
        List<JobType> jobTypeList = homeJobService.getJobTypeByIndustryId(job.getJobType().getIndustry_id());
        model.addAttribute("jobTypeList",jobTypeList);
        List<Degree> degreeList = homeJobService.getDegree();
        model.addAttribute("degreeList",degreeList);
        List<SalaryType> salaryTypeList = homeJobService.getSalaryType();
        model.addAttribute("salaryTypeList",salaryTypeList);
        List<JobTag> jobTagList = homeJobService.getJobTab(job.getCompany_id());
        model.addAttribute("jobTagList",jobTagList);
        List<Zone> zoneList = homeJobService.getZone();
        model.addAttribute("zoneList",zoneList);
        List<City> cityList = homeJobService.getCityByZoneId(job.getCity().getZone_id());
        model.addAttribute("cityList",cityList);
        model.addAttribute("tagList",jobService.getTagForJob(job.getJob_id()));
        return "home/job/edit";
    }

    @RequestMapping("/save")
    public String save(HttpSession session, Model model, Job job) throws Exception
    {
        try{
            int company_id = (Integer) session.getAttribute("company_id");
            int status = homeCompanyService.selectStatusByCompanyId(company_id);
            int detailStatus = homeCompanyService.selectDetailStatusByCompanyId(company_id);
            if (status==1 && detailStatus==1) {
                Date d = new Date();
                job.setDate_modified(d);
                jobService.updateJob(job);
                System.out.println(job.getTag());
                model.addAttribute("success", 1);
                return "forward:/home/job/edit.action";
            }else {
                model.addAttribute("status",status);
                model.addAttribute("detailStatus",detailStatus);
                return "forward:/home/job/edit.action";
            }
        }catch (Exception e){
            model.addAttribute("error", 1);
            return "forward:/home/job/edit.action";
        }
    }

    @RequestMapping("search")
    public String search (Model model) throws Exception
    {
        List<Zone> zoneList = commonService.getZone();
        List<Industry> industryList = commonService.getIndustry();
        List<JobProperty> jobPropertyList = commonService.getJobProperty();
        List<Degree> degreeList = commonService.getDegree();
        List<SalaryType> salaryTypeList = commonService.getSalaryType();

        model.addAttribute("zoneList",zoneList);
        model.addAttribute("industryList",industryList);
        model.addAttribute("jobPropertyList",jobPropertyList);
        model.addAttribute("degreeList",degreeList);
        model.addAttribute("salaryTypeList",salaryTypeList);
        return "home/job/search";
    }

    @RequestMapping("searchJob")
    public String searchJob(Model model, SearchJobPage searchJobPage) throws Exception
    {

        System.out.println(searchJobPage);
        searchJobPage.setPageSize(8);
        searchJobPage.setCurrentPage(1);
        searchJobPage.setSearch_name(searchJobPage.getSearch_name().trim());
        int rows = homeJobService.countRowsForSearchJob(searchJobPage);
        System.out.println(rows);
        if (rows == 0) {
            model.addAttribute("error",1);
            return "forward:/home/job/search.action";
        }
        searchJobPage.setTotalRows(rows);
        List<Job> jobList = homeJobService.selectJob(searchJobPage);
        System.out.println(jobList);
        model.addAttribute("jobList", jobList);
        model.addAttribute("searchJobPage", searchJobPage);
        return "home/job/searchJob";
    }

    @RequestMapping("/getSearchList")
    @ResponseBody
    public Map getSearchList(@RequestParam("p") Integer p, SearchJobPage searchJobPage) throws Exception
    {
        searchJobPage.setPageSize(8);
        searchJobPage.setCurrentPage(p);
        searchJobPage.setSearch_name(searchJobPage.getSearch_name().trim());
        int rows = homeJobService.countRowsForSearchJob(searchJobPage);
        System.out.println(rows);
        searchJobPage.setTotalRows(rows);
        List<Job> jobList = homeJobService.selectJob(searchJobPage);
        Map map = new HashMap();
        map.put("data",jobList);
        map.put("show",searchJobPage);
        return map;
    }


    @RequestMapping("/getResumeList")
    @ResponseBody
    public Map getResumeList(HttpSession session, @RequestParam("p") Integer p) throws Exception
    {
        int customer_id = (Integer) session.getAttribute("customer_id");
        ResumePage resumePage = new ResumePage();
        resumePage.setPageSize(5);
        resumePage.setCurrentPage(p);
        if(customer_id>0){
            resumePage.setCustomer_id(customer_id);
        }
        resumePage.setTotalRows(homeResumeService.totalResumeRows(resumePage));
        List<Resume> resumes = homeResumeService.selectResumeForPage(resumePage);
        Map map = new HashMap();
        map.put("data",resumes);
        map.put("show",resumePage);
        System.out.println(resumes);
        return map;
    }

    @RequestMapping("/sendResume")
    @ResponseBody
    public int sendResume(HttpSession session, @RequestParam("resume_id") int resume_id, @RequestParam("job_id") int job_id) throws Exception
    {
        try {
            if (resume_id <= 0 && job_id <= 0) {
                return 0;
            }
            if (homeJobService.checkIsSend(resume_id, job_id) == 0) {
                int customer_id = (Integer) session.getAttribute("customer_id");
                Delivery delivery = new Delivery();
                delivery.setCustomer_id(customer_id);
                delivery.setResume_id(resume_id);
                delivery.setJob_id(job_id);
                delivery.setCompany_id(homeJobService.selectCompanyIdByJobId(job_id));
                homeJobService.insertDelivery(delivery);
                return 1;
            }else {
                return -1;
            }

        }catch (Exception e){
            return 0;
        }
    }

    @RequestMapping("manager")
    public String manager() throws Exception
    {
        return "home/resume/manager";
    }
}
